import { Component, OnInit, ViewChild } from '@angular/core';

import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';
import { InitService } from 'app/public/serve/init/init.service';
import { Service } from 'app/server/server';

import { ApplicationTableComponent } from './application-table/application-table.component';
import { ApplicationNewlyAddedComponent } from './application-newly-added/application-newly-added.component';

import { app } from './application.data';
import { _timeFormat } from './application.function';
import { appUrl } from './application.url';

declare var $:any;

@Component({
  selector: 'app-application',
  templateUrl: './application.component.html',
  styleUrls: ['./application.component.css']
})

export class ApplicationComponent implements OnInit {

  @ViewChild("table") table : ApplicationTableComponent;
  @ViewChild("add")   add   : ApplicationNewlyAddedComponent;

  app: object = app;

  keySecret: Array<Object>;  

  right: any = { "system" : {}, "application" : {} };
  constructor(
    public initService: InitService,
    public removeTableService: RemoveTableService,
    private request: CudTableService,
    private server:Service
  ) {
    this.right = $.extend(this.right, server.getRight());
  }

  // 侧边栏收缩（展开）
  isActive= this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any) => { this.isActive=value; });    
  }

  /**
   * 分页
   * @param pagination 
   */ 
  _pagination(pagination: Array<any>) {

    app.pagination['pageNumber'] = pagination[0].pageNumber;
    app.pagination['pageSize']   = pagination[0].pageSize;
    app.pagination['naturalNum'] = (app.pagination['pageNumber'] - 1) * app.pagination['pageSize'];

    this.table._table();
  }

  /**
   * 新增默认数据
   */   
  getInit() {

    let data = { optType : 1 };

    this.request
        .postRequest(appUrl.addInit, data)
        .then(data => {

          if (data.result_code == '0000') {
            app.addInit = this.keySecret = [
              { 'appKey'          : data.result.appKey },
              { 'appSecret'       : data.result.appSecret },
              { 'authType'        : data.result.authType },
              { 'scopes'          : data.result.scopes },
              { 'institutionName' : app.tree['institutionName'] }
            ];
            
          }
          this.add._isGrantType();
          return

        })
        .catch(err => {
          return
        })
  }
  
  /**
   * 检索
   * @param info 
   */ 
  _retrieval(info: object) {
    app.retrieval['queryCriteria']   = info['queryCriteria'].trim();
    app.retrieval['startCreateTime'] = _timeFormat(info['startCreateTime']);
    app.retrieval['endCreateTime']   = _timeFormat(info['endCreateTime']);
    app.retrieval['queryType']       = 1;
    this.table._table();
  }

}